package com.example.pay.controller;

import com.example.pay.domain.Time;
import com.example.pay.service.TimeServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description:
 * @Date: Created in 15:44 2020/12/7
 * @Modefied By:
 */
@RestController
@RequestMapping(value = "/time")
public class TimeController {
    @Resource
    TimeServiceImpl timeService;

    Map map = new HashMap();


    /**
    * @Date 16:21 2020/12/8
    * @desc: 添加时间
    */
    @RequestMapping("/addTime")
    public Object add(HttpServletRequest request, @RequestParam String timmer, @RequestParam String tip){
        String openId = request.getHeader("openId");
        int insertTime = timeService.addTime(openId, timmer, tip);
        if(insertTime>0){
            map.put("code",200);
            map.put("msg","添加成功");
        }else{
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return  map;

    }


    /**
    * @Date 16:21 2020/12/8
    * @desc: 删除时间
    */
    @RequestMapping("/delTime")
    public Object del(@RequestParam int id){
        int delById  =timeService.delTime(id);
        if(delById>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }


    @RequestMapping("/getAll")
    public Object getAll(HttpServletRequest request){
        System.out.println("head:"+request.getHeader("openId"));
        String openId = request.getHeader("openId");
        List<Time> list = timeService.queryAll(openId);
        if(list.size()!=0){
            return list;
        }
        return null;
    }
}
